package com.aegean.MonitorExamSYS.center.business.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExamSYS.center.component.ConductionInfoForExaminee;
import com.aegean.MonitorExamSYS.center.component.PositionInfoForExaminee;
import com.aegean.MonitorExamSYS.center.component.SelectCity;
import com.aegean.MonitorExamSYS.center.component.SelectConductionInfo;
import com.aegean.MonitorExamSYS.center.component.SelectExamInfo;
import com.aegean.MonitorExamSYS.center.component.SelectExaminee;
import com.aegean.MonitorExamSYS.center.component.SelectPosition;
import com.aegean.MonitorExamSYS.center.dao.CenterDao;
import com.aegean.MonitorExaxSYS.model.BigType;
import com.aegean.MonitorExaxSYS.model.CenterValue;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.ExamPosition;
import com.aegean.MonitorExaxSYS.model.ExamPositionQualification;
import com.aegean.MonitorExaxSYS.model.ExamineeQualification;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.MessageBox;
import com.aegean.MonitorExaxSYS.model.MonitorQualification;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.PositionQualification;
import com.aegean.MonitorExaxSYS.model.SmallType;

@Service
@Transactional
public class CenterBusinessImpl implements CenterBusiness {
	@Resource
	CenterDao dao;

	@Override
	public void insertService(CenterValue service) {
		dao.insertService(service);
	}

	@Override
	public CenterValue searchService(CenterValue service) {
		// TODO Auto-generated method stub
		if (service.getUser_Login() == null
				|| service.getUser_Password() == null
				|| service.getUser_Login().length() == 0
				|| service.getUser_Password().length() == 0) {
			return null;
		} else {
			CenterValue result = dao.searchService(service);
			return result;
		}

	}

	@Override
	public int searchBigTypeCount() {
		return dao.searchBigTypeCount();
	}

	@Override
	public List<BigType> searchPartBigTypes(int start, int limit) {
		return dao.searchPartBigTypes(start, limit);
	}

	@Override
	public boolean searchBigType(String name) {
		boolean result = dao.searchBigType(name);
		if (result == true) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean searchSmallType(String name) {
		boolean result = dao.searchSmallType(name);
		if (result == true) {
			return false;
		} else {
			return true;
		}
	}

	public boolean addBigType(BigType one) {
		return dao.addBigType(one);
	}

	public boolean addSmallType(SmallType one, int bigType_Id) {
		return dao.addSmallType(one, bigType_Id);
	}

	@Override
	public boolean editBigType(BigType one) {
		// TODO Auto-generated method stub
		return dao.editBigType(one);
	}

	public boolean editSmallType(SmallType one) {
		// TODO Auto-generated method stub
		return dao.editSmallType(one);
	}

	@Override
	public boolean deleteBigType(String[] big_Name) {
		return dao.deleteBigType(big_Name);
	}

	@Override
	public int searchSmallTypeCount(String bigName) {
		return dao.searchSmallTypeCount(bigName);
	}

	@Override
	public List<SmallType> searchPartSmallTypes(String bigName, int start,
			int limit) {
		return dao.searchPartSmallTypes(bigName, start, limit);
	}

	@Override
	public boolean deleteSmallType(int[] small_Ids) {
		// TODO Auto-generated method stub
		return dao.deleteSmallType(small_Ids);
	}

	public boolean searchExamValue(String name) {
		boolean result = dao.searchExamValue(name);
		if (result == true) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean addExamValue(ExamInfo one, int Small_Id) {
		// TODO Auto-generated method stub

		return dao.addExamValue(one, Small_Id);
	}

	@Override
	public int searchExamValueCount(int small_Id) {
		// TODO Auto-generated method stub
		return dao.searchExamValueCount(small_Id);
	}

	@Override
	public List<ExamInfo> searchPartExamValues(int small_Id, int start,
			int limit) {
		// TODO Auto-generated method stub
		return dao.searchPartExamValues(small_Id, start, limit);
	}

	@Override
	public boolean editExamValue(ExamInfo one, File photo) {
		// TODO Auto-generated method stub
		return dao.editExamValue(one, photo);
	}

	@Override
	public boolean deleteExamValue(int[] Exam_Ids) {
		return dao.deleteExamValue(Exam_Ids);
	}

	@Override
	public List<ExamInfo> searchLeastExams() {
		// TODO Auto-generated method stub
		return dao.searchLeastExams();
	}

	@Override
	public List<SelectExamInfo> showExanList(int small_Id, int state)
			throws Exception {
		return dao.showExanList(small_Id, state);
	}

	@Override
	public List<SelectConductionInfo> showConductionList(int exam_Id,
			int state, Date startTime, Date endTime) throws Exception {
		// TODO Auto-generated method stub
		return dao.showConductionList(exam_Id, state, startTime, endTime);
	}

	@Override
	public List<ExamPosition> showPositionList(int unit_Id, Date startTime,
			Date endTime) {
		// TODO Auto-generated method stub
		return dao.showPositionList(unit_Id, startTime, endTime);
	}

	@Override
	public Map<Integer, List<ExamPosition>> showPositionLists(Object[] ids,
			Date startTime, Date endTime) {
		Map<Integer, List<ExamPosition>> maps = new HashMap<Integer, List<ExamPosition>>();
		for (Object obj : ids) {
			maps.put(Integer.parseInt(obj.toString()),
					new ArrayList<ExamPosition>());
		}
		// TODO Auto-generated method stub
		List<ExamPosition> list = dao
				.showPositionLists(ids, startTime, endTime);
		for (ExamPosition examPosition : list) {
			maps.get(examPosition.getExamPosition_Unit().getConduction_Id())
					.add(examPosition);
		}
		return maps;
	}

	@Override
	public boolean confirmArrageForCP(Map<Integer, List<Integer>> cp,
			Map<Integer, Integer> cpc, int exam_Id, Date exam_StartTime) {
		// TODO Auto-generated method stub
		return dao.confirmArrageForCP(cp, cpc, exam_Id, exam_StartTime);
	}

	@Override
	public List<ExamInfo> showExanListForMonitorAndExaminee(int small_Id,
			int state, int status) throws Exception {
		// TODO Auto-generated method stub
		return dao.showExanListForMonitorAndExaminee(small_Id, state, status);
	}

	@Override
	public List<SelectPosition> showPositionListForMonitor(int exam_Id) {
		// TODO Auto-generated method stub
		return dao.showPositionListForMonitor(exam_Id);
	}

	@Override
	public List<MonitorValue> showMonitorsListForMonitor(int unit_Id,
			Date startTime, Date endTime) {
		// TODO Auto-generated method stub
		List<MonitorValue> monitors = dao.showMonitorsListForMonitor(unit_Id,
				startTime, endTime);
		List<BigType> bigs = dao.searchBigList();
		for (MonitorValue one : monitors) {
			for (BigType big : bigs) {
				if (one.getMonitor_Big().getBig_Id() == big.getBig_Id()) {
					one.setMonitor_Big(big);
					break;
				} else {
					continue;
				}
			}
		}
		return monitors;
	}

	@Override
	public boolean confirmArrangeMonitors(int exam_Id, Date startTime,
			Map<Integer, List<Integer>> maps) {
		// TODO Auto-generated method stub
		return dao.confirmArrangeMonitors(exam_Id, startTime, maps);
	}

	@Override
	public int searchBigId(int exam_Id) {
		// TODO Auto-generated method stub
		return dao.searchBigId(exam_Id);
	}

	@Override
	public List<ConductionInfoForExaminee> showConductionListForExaminee(
			int exam_Id) {
		// TODO Auto-generated method stub
		return dao.showConductionListForExaminee(exam_Id);
	}

	@Override
	public List<PositionInfoForExaminee> showPositionListForExaminee(int exam_Id,
			int con_Id) {
		// TODO Auto-generated method stub
		return dao.showPositionListForExaminee(exam_Id, con_Id);
	}

	@Override
	public List<SelectExaminee> showExamineesList(int exam_Id) {
		// TODO Auto-generated method stub
		return dao.showExamineesList(exam_Id);
	}

	@Override
	public boolean confirmExaminees(
			Map<Integer, Map<Integer, List<SelectExaminee>>> maps, Date time,int exam_Id) {
		// TODO Auto-generated method stub
		return dao.confirmExaminees(maps,time,exam_Id);
	}

	public List<SelectCity> showCitys() {
		// TODO Auto-generated method stub
		return dao.showCitys();
	}

	@Override
	public List<ConductionValue> showConductions(String city_Name,int start,int limit) {
		// TODO Auto-generated method stub
		return dao.showConductions(city_Name,start,limit);
	}

	@Override
	public boolean writeMessage(int conduction_Id, String theme, String contents) {
		// TODO Auto-generated method stub
		return dao.writeMessage(conduction_Id, theme, contents);
	}

	@Override
	public List<MessageBox> showMessages(int state, int status,
			int conduction_Id,int start,int limit) {
		// TODO Auto-generated method stub
		return dao.showMessages(state, status, conduction_Id,start,limit);
	}

	@Override
	public boolean replyMessage(int conduction_Id, String theme,
			String contents, int beforeId) {
		// TODO Auto-generated method stub
		return dao.replyMessage(conduction_Id, theme, contents, beforeId);
	}
}
